package com.lagou.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wsl
 * @Date 2021/11/7 19:15
 * @Project_Name jdbc_spring
 */
public class BeanFactory {

    private static Map<String,Object> icoMap = new HashMap<>();

    //程序启动时，初始化对象实例
    static {

        //1.读取配置文件
        InputStream resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");

        //2.解析xml（使用dom4j）
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(resourceAsStream);

            //3.编写xpath表达式
            String xpath = "//bean";

            //4.获取到所有的bean标签
            List<Element> list = document.selectNodes(xpath);

            //5.遍历并使用反射创建实例对象，设置到map （IOC容器）集合中
            for (Element element : list) {
                String id = element.attributeValue("id");
                //className: com.lagou.dao.impI.UserDaoImpI
                String className = element.attributeValue("class");
                //使用反射生成实例对象
                Object o = Class.forName(className).newInstance();
                //存到map中，key:id value:o
                icoMap.put(id,o);
            }
        } catch (DocumentException | ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

    }

    //获取指定id的对象实例
    public static Object getBean(String beanId){
        Object o = icoMap.get(beanId);
        return o;
    }

}
